//
//  ImageViewController.m
//  BeautyClother
//
//  Created by dllo on 15/9/3.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "ImageViewController.h"
#import "ImageLayout.h"
#import "UIImageView+WebCache.h"
#import "define.h"
#import "AsyncConnection.h"

@interface ImageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) ImageLayout *layout;

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;

@end

@implementation ImageViewController

- (void)dealloc
{
    [_layout release];
    [_collection release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createCollection];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}

- (void)handleData
{
    
    
    NSDictionary *dic = @{@"flag":@""};
    [AsyncConnection asyncConnection:IMAGE parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [[dic objectForKey:@"data"]objectForKey:@"backgrounds"];
        [self.collection reloadData];
        
    }path:@"backgroungImage"];
    self.layout = [[ImageLayout alloc]init];
    if (self.array.count % 3 == 0) {
        _layout.height = self.array.count / 3 * 130 * _offHeight;
    }else
    {
        
        _layout.height = (self.array.count / 3 + 1) * 130 * _offHeight;
    }

    
}



- (void)createCollection
{
    
    
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width , self.view.frame.size.height - 70) collectionViewLayout:_layout];
    _layout.itemSize = CGSizeMake(80 * _offWidth, 100 * _offHeight);
    _layout.minimumLineSpacing = 20 *_offWidth;
    _layout.minimumInteritemSpacing = 20 * _offHeight;
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    [self.view addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"rrr"];
    [self.collection release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
        return self.array.count;
    //    return ((NSArray *)[[self.numArr objectAtIndex:self.cid.intValue]objectForKey:@"hot_country"]).count + ((NSArray *)[[self.numArr objectAtIndex:self.cid.intValue]objectForKey:@"country"]).count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rrr" forIndexPath:indexPath];
    NSDictionary *dic = [self.array objectAtIndex:indexPath.item];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
    [cell.contentView addSubview:image];
    [image sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"thumbnailUrl"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    image.layer.cornerRadius = 5;
    image.layer.masksToBounds = YES;
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate passImage:[[self.array objectAtIndex:indexPath.item]objectForKey:@"url"]];
    [self.navigationController popToRootViewControllerAnimated:YES];
//    DetailViewController *detail = [[DetailViewController alloc]init];
//    
//    detail.tid = [[self.countryArr objectAtIndex:indexPath.item]objectForKey:@"id"];
//    detail.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:detail animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
